//			function change() {
//				var lis = document.getElementsByTagName("a");
//
//				for (var j = 0; j < lis.length; j++) {
//					lis[j].onmouseover = function() {
//						this.className = "active";
//					}
//						
//					lis[j].onmouseout = function() {
//						this.className = "";
//					}
//	
//				}
//			}
$(document).ready(function() {

	$.fn.printCutomerDetails = function(peoId) {
		$.post("../../UserInfoManagementServlet", {
			"Type": "Customer",
			"peoId": peoId
		}, function(ret) {
			if (ret != null) {
				console.log(ret);
				$("#cusId").val(ret.cusId);
				$("#cusName").val(ret.cusName);
				$("#cusNameRow").val(ret.cusName);
				$("#corpoRepresentative").val(ret.corpoRepresentative);
				$("#corpoRepresentativeRow").val(ret.corpoRepresentative);
				$("#regiAddress").val(ret.regiAddress);
				$("#regiAddressRow").val(ret.regiAddress);
				$("#email").val(ret.email);
				$("#emailRow").val(ret.email);
				$("#myProfileisGet").val("true");
			}
		});
	}
	//显示客户基本信息
	$("#myProfile").click(function() {
		var peoId = $("#peoId").html();
		if ($("#myProfileisGet").val() == "false") {
			$().printCutomerDetails(peoId);
		}
	});

	//显示当前信用等级
	$("#myCredit").click(function() {
		if ($("#myCreditisGet").val() == "false") {
			$.post("../../CusCreditLevelInfoServlet", {
				"Type": "Now",
				"peoId": $("#peoId").html()
			}, function(ret) {
				if (ret != null) {
					console.log(ret);
					$("#myCreditTable").append(
						"<tr>" +
						"<td>" + "当前信用等级" + "</td>" +
						"<td>" + ret.cusId + "</td>" +
						"<td>" + ret.creditLevel + "</td>" +
						"<td>" + ret.thisLevelBeginDate + "</td>" +
						"<td>" + ret.thisLevelEndDate + "</td>" +
						"</tr>"
					);
					$("#myCreditisGet").val("true");
				}
			});
		}
	});
	//循环输出到表格
	$.fn.appendToTable = function(ret) {
		$("tr[name='history']").remove();
		for (let i = 0; i < ret.length; i++) {
			$("#myCreditTable").append(
				"<tr name = 'history'>" +
				"<td>" + ret[i].levelHisId + "</td>" +
				"<td>" + ret[i].cusId + "</td>" +
				"<td>" + ret[i].creditLevel + "</td>" +
				"<td>" + ret[i].thisLevelBeginDate + "</td>" +
				"<td>" + ret[i].thisLevelEndDate + "</td>" +
				"</tr>"
			);
		}
	}
	//显示所有的历史信用
	$("#showAllCredit").click(function() {
		if ($("#myAllCreditisGet").val() == "false") {
			$.post("../../CusCreditLevelInfoServlet", {
				"Type": "History",
				"peoId": $("#peoId").html()
			}, function(ret) {
				if (ret != null) {
					console.log(ret);
					$().appendToTable(ret);
					$("#myAllCreditisGet").val("true");
				} else {

					swal({
						title: "无数据！",
						text: "您的信用历史记录为空！",
						type: "warning",
					});
				}
			});
		}
	});
	//刷新历史信用
	$("#refreshHis").click(function() {
		$(this).attr("disabled", true);
		$.post("../../CusCreditLevelInfoServlet", {
			"Type": "Refresh",
			"peoId": $("#peoId").html()
		}, function(ret) {
			if (ret != null) {
				console.log(ret);
				$().appendToTable(ret);
			}
		});
	});
	//退出登录
	$("#logout").click(function() {
		swal({
			title: "退出？",
			text: "尊敬的" + $("#peoId").html() + "您即将退出系统，我们不会为你保存您未保存的数据，您确定吗？",
			showConfirmButton: true,
			showCancelButton: true,
			confirmButtonText: "退出",
			cancelButtonText: "取消",
			closeOnConfirm: false,
			type: "warning",
		}, function(isConfirm) {
			if (isConfirm) {
				$.post("../../LoginServlet", {
					"Type": "logout",
				}, function(ret) {
					if (ret != null) {

						swal({
							title: "退出成功！",
							text: "您已成功退出系统，期待您的下次使用！",
							showConfirmButton: false,
							type: "success",
						});

						setTimeout("$(location).attr('href', '../mylogin.html')", 2000);
					}
				});
			}
		});
	});
	//查询信贷资格
	$("#CreditCheck").click(function() {
		var peoId = $("#peoId").html();
		$.post("../../CusLoanServlet", {
			"Type": "LoanTypeCheck",
			"peoId": peoId
		}, function(ret) {
			console.log(ret);
			if (ret.split(" ")[0] != "null") { //额度足够
				$("#myCreditLevel").html(ret.split(" ")[1]);
				swal({
					title: "查询成功！",
					text: "尊敬的" + peoId + "客户，您好！您现在的信用额度为" + ret.split(" ")[1] + "\n现在进行信用贷吗？",
					showConfirmButton: true,
					showCancelButton: true,
					cancelButtonText: "算了",
					confirmButtonText: "好的",
					type: "success",
				}, function(isConfirm) {
					if (isConfirm) {
						$("#LoanTableChoose").css("display", "inline");
					}
				});
			} else { //额度不足
				if (ret.split(" ")[1] == "null") {
					swal({ //不可贷款
						title: "查询成功！",
						text: "尊敬的" + peoId + "客户，您好！\n很抱歉！您现在的信用额度不足,努力提升信用值吧！",
						showConfirmButton: true,
						confirmButtonText: "好的",
						type: "warning",
					});
				} else { //质押物贷款
					swal({
						title: "查询成功！",
						text: "尊敬的" + peoId + "客户，您好！很抱歉！您现在的信用额度不足以进行信用贷，您可以点击下方按钮进行质押物贷款！",
						showConfirmButton: true,
						confirmButtonText: "贷款",
						showCancelButton: true,
						cancelButtonText: "算了",
						type: "success",
					}, function(isConfirm) {
						$("#LoanTableChoose").css("display", "inline");
						$("#CreditOnly").css("display", "none");
						$("#Both").css("display", "none");
					});
				}

			}

		});
	});
	//初始化所有输入框
	$.fn.initAllToNone = function() {
		$("#ensureCommit").css("display", "none");
		$("#tipsWrong").css("display", "none");
		$("#CusWantLoan").val("");
		$("#CusWantLoanDeposit").val("");
		$("#CusWantLoanDepositPrice").val("");
		$("#CusWantLoanDepositNum").val("");
	}
	//仅信用贷
	$("#CreditOnly").click(function() {
		if ($("#typeOfLoan").val() != "") {
			if ($("#typeOfLoan").val() != "CreditOnly") {
				$().initAllToNone();
				$("#typeOfLoan").val("CreditOnly");
			}
		} else {
			$("#typeOfLoan").val("CreditOnly");
		}

		$("#LoanTableDiv").css("display", "block");
		$("tr[name='DepositOnlyTable']").css("display", "none");
		$("tr[name='CreditOnlyTable']").css("display", "block");
	});
	//仅质押物贷款
	$("#DepositOnly").click(function() {
		if ($("#typeOfLoan").val() != "") {
			if ($("#typeOfLoan").val() != "DepositOnly") {
				$().initAllToNone();
				$("#typeOfLoan").val("DepositOnly");
			}
		} else {
			$("#typeOfLoan").val("DepositOnly");
		}
		$().initAllToNone();
		$("#LoanTableDiv").css("display", "block");
		$("tr[name='CreditOnlyTable']").css("display", "none");
		$("tr[name='DepositOnlyTable']").css("display", "block");

	});
	//混合贷
	$("#Both").click(function() {
		if ($("#typeOfLoan").val() != "") {
			if ($("#typeOfLoan").val() != "Both") {
				$().initAllToNone();
				$("#typeOfLoan").val("Both");
			}
		} else {
			$("#typeOfLoan").val("Both");
		}
		$().initAllToNone();

		$("#LoanTableDiv").css("display", "block");
		$("tr[name='CreditOnlyTable']").css("display", "block");
		$("tr[name='DepositOnlyTable']").css("display", "block");

	});
	//信用贷数额检查
	$("#CusWantLoan").change(function() {
		var max = parseInt($("#myCreditLevel").html());

		if (/\D/.test($("#CusWantLoan").val())) {
			$("#tipsWrong").html("<i class='fa fa-remove'></i>数额包含非数字，请检查");
			$("#tipsWrong").css("display", "inline");
			$("#ensureCommit").css("display", "none");
			return;
		} else {
			$("#tipsWrong").css("display", "none");
			$("#ensureCommit").css("display", "inline");
		}
		var input = parseInt($("#CusWantLoan").val());
		if (!(input <= max && input > 0)) {
			$("#tipsWrong").html("<i class='fa fa-remove'></i>贷款数额不正确，请核对后重新填写");
			$("#tipsWrong").css("display", "inline");
			$("#ensureCommit").css("display", "none");
			return;
		} else {

			$("#tipsWrong").css("display", "none");
			$("#ensureCommit").css("display", "inline");
		}
	});
	//质押物预估价格检查
	$("#CusWantLoanDepositPrice").change(function() {
		if (/\D/.test($("#CusWantLoanDepositPrice").val())) {
			$("#tipsWrong").html("<i class='fa fa-remove'></i>价格包含非数字，请检查");
			$("#tipsWrong").css("display", "inline");
			$("#ensureCommit").css("display", "none");
			return;
		} else {
			$("#tipsWrong").css("display", "none");
		}

	});
	//质押物数量
	$("#CusWantLoanDepositNum").change(function() {
		if (/\D/.test($("#CusWantLoanDepositNum").val())) {
			$("#tipsWrong").html("<i class='fa fa-remove'></i>数量包含非数字，请检查");
			$("#tipsWrong").css("display", "inline");
			$("#ensureCommit").css("display", "none");
		} else {
			$("#tipsWrong").css("display", "none");
			$("#ensureCommit").css("display", "inline");
		}
	});
	//贷款提交按钮
	$("#ensureCommit").click(function() {
		var typeAA = $("#typeOfLoan").val();
		var peoId = $("#peoId").html();
		alert(typeAA)
		if (typeAA == "CreditOnly") {
			var CusWantNum = $("#CusWantLoan").val();
			$.post("../../CusLoanServlet", {
				"Type": "CreditOnly",
				"peoId": peoId,
				"CusWantNum": CusWantNum
			}, function(ret) {
				alert(ret)
				if (ret.split(" ")[0] != "null") {
					swal({
						title: "贷款成功！",
						text: "您的贷款金额是：" + ret + "\n请注意查收款项！",
						type: "success",
					});
				} else {
					swal({
						title: "贷款失败！",
						text: "请咨询客服！或检查您是否有正在进行的贷款！",
						type: "error",
					});
				}
			});
		}
		if (typeAA == "DepositOnly") {
			var CusWantLoanDeposit = $("#CusWantLoanDeposit").val();
			var CusWantLoanDepositPrice = $("#CusWantLoanDepositPrice").val();
			var CusWantLoanDepositNum = $("#CusWantLoanDepositNum").val();
			$.post("../../CusLoanServlet", {
				"Type": "DepositOnly",
				"peoId": peoId,
				"CusWantLoanDeposit": CusWantLoanDeposit,
				"CusWantLoanDepositPrice": CusWantLoanDepositPrice,
				"CusWantLoanDepositNum": CusWantLoanDepositNum
			}, function(ret) {
				alert(ret)

				if (ret == "Max") {
					swal({
						title: "贷款失败！",
						text: "质押物贷款达上限7个",
						type: "error",
					});

				} else if (ret != "null") {
					swal({
						title: "贷款成功！",
						text: "您的预估贷款金额是：" + CusWantLoanDepositPrice + "\n已为您分配经手经理,工号是" + ret + "，他会尽快联系您，请留意手机来电！",
						type: "success",
					});
				} else {
					swal({
						title: "贷款失败！",
						text: "请咨询客服！或检查您是否有正在进行的贷款！",
						type: "error",
					});
				}
			});
		}
		if (typeAA == "Both") {
			var CusWantNum = $("#CusWantLoan").val();
			var CusWantLoanDeposit = $("#CusWantLoanDeposit").val();
			var CusWantLoanDepositPrice = $("#CusWantLoanDepositPrice").val();
			var CusWantLoanDepositNum = $("#CusWantLoanDepositNum").val();
			$.post("../../CusLoanServlet", {
				"Type": "Both",
				"peoId": peoId,
				"CusWantNum": CusWantNum,
				"CusWantLoanDeposit": CusWantLoanDeposit,
				"CusWantLoanDepositPrice": CusWantLoanDepositPrice,
				"CusWantLoanDepositNum": CusWantLoanDepositNum
			}, function(ret) {
				alert(ret)
				alert(ret.split("+")[0].split(" ")[1] + "  " + ret.split("+")[1].split(" ")[1])
				if (ret.split("+")[0].split(" ")[1] != "null" && ret.split("+")[0].split(" ")[1] != "Max") {
					if (ret.split("+")[1].split(" ")[1] != "null") {
						swal({
							title: "双重贷款成功！",
							text: "您的预估贷款金额是：" + ret.split("+")[1].split(" ")[1] + "\n已为您分配经手经理,工号是" + ret.split("+")[0].split(" ")[
									1] +
								"，我们会尽快与您取得联系，请留意手机来电！",
							type: "success",
						});
					}
				}
				console.log(ret.split("+")[1].split(" ")[1] != "null")
				if (ret.split("+")[1].split(" ")[1] != "null") {
					swal({
						title: "信用贷款成功！",
						text: "您的预估贷款金额是" + ret.split("+")[1].split(" ")[1],
						closeOnConfirm: false,
						type: "sueecss",
					}, function(isConfirm) {
						if (confirm) {
							$().DepositShowTips(ret);
						}
					});
				} else {
					swal({
						title: "信用贷款失败！",
						text: "请咨询客服！或检查您是否有正在进行的贷款！",
						closeOnConfirm: false,
						type: "error",
					}, function(isConfirm) {
						if (confirm) {
							$().DepositShowTips(ret);
						}
					});
				}

				// if (ret.split("+")[1].split(" ")[1] != "null") {
				// 	if (ret.split("+")[0].split(" ")[1] != "null") {
				// 		swal({
				// 			title: "双重贷款成功！",
				// 			text: "您的预估贷款金额是：" + ret.split("+")[1].split(" ")[1] + "\n已为您分配经手经理,工号是" + ret.split("+")[0].split(" ")[
				// 					1] +
				// 				"，我们会尽快与您取得联系，请留意手机来电！",
				// 			type: "success",
				// 		});
				// 	} else {
				// 		swal({
				// 			title: "质押物贷款失败！信用贷成功，您的预估贷款金额是" + ret.split("+")[1].split(" ")[1],
				// 			text: "请咨询客服！或检查您是否有正在进行的贷款！",
				// 			type: "error",
				// 		});
				// 	}

				// } else {
				// 	swal({
				// 		title: "信用贷款失败！",
				// 		text: "请咨询客服！或检查您是否有正在进行的贷款！",
				// 		type: "error",
				// 	});
				// }
			});
		}
	});
	//显示质押物提示框
	$.fn.DepositShowTips = function(ret) {
		if (ret.split("+")[0].split(" ")[1] != "null" && ret.split("+")[0].split(" ")[1] != "Max") {
			swal({
				title: "质押物贷款成功！",
				text: "已为您分配经手经理,工号是" + ret.split("+")[0].split(" ")[1],
				closeOnConfirm: false,
				type: "success",
			});
		} else {
			swal({
				title: "质押物贷款失败！",
				text: "请咨询客服！或检查您是否有正在进行的贷款！或已达上限！",
				closeOnConfirm: false,
				type: "error",
			});
		}
	}
	//新密码检验
	$("#newPWD").change(function() {
		var first = $("#newPWD").val();
		var second = $("#repeatPWD").val();
		if (first.length < 6 || first.length > 20) {
			$("#pwdtipswrong").html("<i class='fa fa-remove'></i>您的新密码长度不符合规定（6-20），请重试！");
			return;
		}
		if (first.replace(/(^\s*)|(\s*$)/g, '') == null || first.replace(/(^\s*)|(\s*$)/g, '') == "") {
			$('#pwdtipswrong').html("<i class='fa fa-remove'></i>新密码不可为空或您没有输入有效的字符！");
			return;
		}
		if (/[^0-9]/.test(first)) {
			$('#pwdtipswrong').html("<i class='fa fa-remove'></i>新密码值不可为非数字字符！");
			return;
		}
		$("#pwdtipswrong").html("");
	});
	//重复密码检验
	$("#repeatPWD").change(function() {
		var first = $("#newPWD").val();
		var second = $("#repeatPWD").val();
		if (first != second) {
			$("#pwdtipswrong").html("<i class='fa fa-remove'></i>两次密码输入不一致");
			return;
		}
		if (second.length < 6 || second.length > 20) {
			$("#pwdtipswrong").html("<i class='fa fa-remove'></i>您的新密码长度不符合规定（6-20），请重试！");
			return;
		}
		if (second.replace(/(^\s*)|(\s*$)/g, '') == null || second.replace(/(^\s*)|(\s*$)/g, '') == "") {
			$('#pwdtipswrong').html("<i class='fa fa-remove'></i>新密码不可为空或您没有输入有效的字符！");
			return;
		}
		if (/[^0-9]/.test(second)) {
			$('#pwdtipswrong').html("<i class='fa fa-remove'></i>新密码值不可为非数字字符！");
			return;
		}
		$("#pwdtipswrong").html("");
		$("#resetPWDByUser").css("display", "inline");

	});
	//重置密码
	$("#resetPWDByUser").click(function() {
		var peoId = $("#peoId").html();
		var PWD = $("#repeatPWD").val();
		$.post("../../UserInfoManagementServlet", {
			"Type": "ChangePWD",
			"peoId": peoId,
			"PWD": PWD
		}, function(ret) {
			alert(ret)
			if (ret != "null") {
				swal({
					title: "修改成功！",
					text: "您的新密码是：" + PWD + "\n请注意牢记！",
					type: "success",
				});
			} else {
				swal({
					title: "修改失败！",
					text: "请稍后重试！",
					type: "error",
				});
			}
		});
	});
	//显示正在进行的贷款
	$("#showLoans").click(function() {
		var peoId = $("#peoId").html();

		if ($("#loanCreditIsGet").val() == "false") {
			$().getCreditLoan();
		}
		if ($("#loanDepositIsGet").val() == "false") {
			$().getDepositLoan();
		}
	});
	//获取正在进行的信用贷
	$.fn.getCreditLoan = function() {
		$("#showLoans").attr("disabled", true);
		console.log($("#showLoans").html());
		$("#showLoans").html("加载信用贷款<i class='fa fa-refresh fa-spin fa-fw'></i>");
		$.post("../../CusLoanServlet", {
			"Type": "FindCredit",
			"peoId": $("#peoId").html()
		}, function(ret) {
			if (ret != null) {
				console.log(ret);
				$("#myLoanTable").append(
					"<tr>" +
					"<td>" + "信用贷款" + "</td>" +
					"<td>" + ret.loanMoney + "</td>" +
					"<td>" + "开始日期" + ret.beginDate + " 信用贷款天数：30" + "</td>" +
					"</tr>"
				);
				$("#showLoans").attr("disabled", false);
				$("#showLoans").html("显示当前进行中的贷款项");
				$("#loanCreditIsGet").val("true");
			} else {
				$("#showLoans").attr("disabled", false);
				$("#showLoans").html("显示当前进行中的贷款项");
				swal({
					title: "数据为空！",
					text: "您的信用贷款已还清或还未信用贷！",
					closeOnConfirm: false,
					type: "warning",
				});
			}
		});
	}
	//获取正在进行的质押物贷款
	$.fn.getDepositLoan = function() {
		$("#showLoans").attr("disabled", true);
		console.log($("#showLoans").html());
		$("#showLoans").html("加载质押物贷款<i class='fa fa-refresh fa-spin fa-fw'></i>");
		$.post("../../CusLoanServlet", {
			"Type": "FindDeposit",
			"peoId": $("#peoId").html()
		}, function(ret) {
			if (ret != null) {
				console.log(ret);
				for (let i = 0; i < ret.length; i++) {
					$("#myLoanTable").append(
						"<tr>" +
						"<td>" + "质押物贷款" + "</td>" +
						"<td>" + ret[i].depGuessPrice + "</td>" +
						"<td>" + "质押物名称：" + ret[i].depName + " 经手经理id：" + ret[i].manId + "</td>" +
						"</tr>"
					);
				}
				$("#showLoans").attr("disabled", false);
				$("#showLoans").html("显示当前进行中的贷款项");
				$("#loanDepositIsGet").val("true");
			} else {
				$("#showLoans").attr("disabled", false);
				$("#showLoans").html("显示当前进行中的贷款项");
				swal({
					title: "数据为空！",
					text: "您的质押物贷款已还清或还未质押物贷！",
					closeOnConfirm: false,
					type: "warning",
				});
			}
		});
	}

	$("#CustomerProfileChange").click(function() {
		if ($("#cusName").val() == $("#cusNameRow").val()) {
			if ($("#corpoRepresentativeRow").val() == $("#corpoRepresentative").val()) {
				if ($("#regiAddressRow").val() == $("#regiAddress").val()) {
					if ($("#emailRow").val() == $("email").val()) {
						swal({
							title: "警告！",
							text: "您没有修改任何数据！",
							type: "warning",
						});
						return;
					}
				}
			}
		}
		var myreg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
		if (!myreg.test($("#email").val())) {
			swal({
				title: "警告！",
				text: "邮箱格式不正确！",
				type: "warning",
			});
			return;
		}




		$.post("../../UserInfoManagementServlet", {
			"Type": "CusChangeProfile",
			"peoId": $("#peoId").html(),
			"cusName": $("#cusName").val(),
			"corpoRepresentative": $("#corpoRepresentative").val(),
			"regiAddress": $("#regiAddress").val(),
			"mailCode": $("#mailCode").val(),
		}, function(ret) {
			console.log(ret)
			if (ret != "null") {
				swal({
					title: "修改成功！",
					text: "您的数据已修改，两秒后为您刷新页面！",
					showConfirmButton: false,
					type: "success",
				});
				setTimeout("location.reload() ", 2000);

			} else {
				swal({
					title: "修改失败！",
					text: "请稍后重试！两秒后为您刷新页面！",
					showConfirmButton: false,
					type: "error",
				});
				setTimeout("location.reload() ", 2000);
			}
		});


	});

});

function showthis(obj) {
	var classes = document.getElementsByName("contenttoshow");
	var lis = document.getElementsByTagName("a");
	for (var i = 0; i < lis.length; i++) {
		lis[i].className = "";
	}
	for (var i = 0; i < classes.length; i++) {
		classes[i].style.display = "none";
	}
	document.getElementById(obj.id).className = "active";
	document.getElementsByClassName(obj.id)[0].style.display = "block";
}

function btncolorover(obj) {
	obj.className = "btn-active";
}

function btncolorout(obj) {
	obj.className = "";
}
